package com.homedo.microservice.distributed.id.service.remote.api.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.homedo.microservice.distributed.id.service.api.IGenerateMonitorServiceRemote;
import com.homedo.microservice.distributed.id.service.service.SegmentService;
import com.sankuai.inf.leaf.segment.SegmentIDGenImpl;
import com.sankuai.inf.leaf.segment.model.LeafAlloc;
import com.sankuai.inf.leaf.segment.model.Segment;
import com.sankuai.inf.leaf.segment.model.SegmentBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class GenerateMonitorServiceRemoteImpl implements IGenerateMonitorServiceRemote {
    private static final Logger _LOGGER = LoggerFactory.getLogger(GenerateMonitorServiceRemoteImpl.class);

    @Autowired
    private SegmentService segmentService;

    @Override
    public Object getCache() {
        SegmentIDGenImpl segmentIDGen = segmentService.getIdGen();
        if (segmentIDGen == null) {
            throw new IllegalArgumentException("You should config generate.id.enable=true first");
        }
        Map<String, SegmentBuffer> cache = segmentIDGen.getCache();
        Set<Map.Entry<String, SegmentBuffer>> cacheSet = cache.entrySet();

        List ret = Lists.newArrayList();

        for (Map.Entry<String, SegmentBuffer> stringSegmentBufferEntry : cacheSet) {
            Map resultMap = Maps.newHashMap();

            String name = stringSegmentBufferEntry.getKey();
            boolean init = stringSegmentBufferEntry.getValue().isInitOk();
            boolean next = stringSegmentBufferEntry.getValue().isNextReady();
            long pos = stringSegmentBufferEntry.getValue().getCurrentPos();


            resultMap.put("name", name);
            resultMap.put("init", init);
            resultMap.put("next", next);
            resultMap.put("pos", pos);
//            resultMap.put("update", new DateTime(stringSegmentBufferEntry.getValue().getUpdateTimestamp()).toString("yyyy-MM-dd HH:mm:ss"));

            Segment[] segments = stringSegmentBufferEntry.getValue().getSegments();

            Segment segment0 = segments[0];
            long value0 = segment0.getValue().get();
            long max0 = segment0.getMax();
            long step0 = segment0.getStep();

            resultMap.put("value0", value0);
            resultMap.put("max0", max0);
            resultMap.put("step0", step0);

            if (segments.length > 1) {
                Segment segment1 = segments[1];
                long value1 = segment1.getValue().get();
                long max1 = segment1.getMax();
                long step1 = segment1.getStep();

                resultMap.put("value1", value1);
                resultMap.put("max1", max1);
                resultMap.put("step1", step1);

            }

            ret.add(resultMap);
        }

        return ret;
    }

    @Override
    public Object getDb() {
        SegmentIDGenImpl segmentIDGen = segmentService.getIdGen();
        if (segmentIDGen == null) {
            throw new IllegalArgumentException("You should config generate.id.enable=true first");
        }
        List<LeafAlloc> items = segmentIDGen.getAllLeafAllocs();
//        _LOGGER.info("DB info {}", items);

        List ret = Lists.newArrayList();

        items.forEach(e -> {
            Map resultMap = Maps.newHashMap();

            resultMap.put("tag", e.getKey());
            resultMap.put("max", e.getMaxId());
            resultMap.put("step", e.getStep());
            resultMap.put("update", e.getUpdateTime());

            ret.add(resultMap);
        });

        return items;
    }
}
